package com.xdk.templatescala.utils.uuid;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * ID生成器工具类
 *
 * @author ruoyi
 */
@Slf4j
@Component
public class IdUtils implements ApplicationContextAware
{

    private static SnowflakeIdGenerator snowflakeIdGenerator;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        snowflakeIdGenerator = applicationContext.getBean(SnowflakeIdGenerator.class);
    }

    /**
     * 获取随机UUID
     *
     * @return 随机UUID
     */
    public static String randomUUID()
    {
        return UUID.randomUUID().toString();
    }

    /**
     * 简化的UUID，去掉了横线
     *
     * @return 简化的UUID，去掉了横线
     */
    public static String simpleUUID()
    {
        return UUID.randomUUID().toString(true);
    }

    /**
     * 获取随机UUID，使用性能更好的ThreadLocalRandom生成UUID
     *
     * @return 随机UUID
     */
    public static String fastUUID()
    {
        return UUID.fastUUID().toString();
    }

    /**
     * 简化的UUID，去掉了横线，使用性能更好的ThreadLocalRandom生成UUID
     *
     * @return 简化的UUID，去掉了横线
     */
    public static String fastSimpleUUID()
    {
        return UUID.fastUUID().toString(true);
    }

    public static String nextId(){
        return String.valueOf(snowflakeIdGenerator.nextId());
    }

//    public static String nextId(){
//        return nextId(0,0);
//    }
//    public static String nextId(long workerId, long dataCenterId){
//        SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(0,0);
//        return String.valueOf(idGenerator.nextId());
//    }

}
